import { ref, onMounted } from 'vue'
import { serviceApi } from '@/api/public/service'
import type { Service } from '@/types/service'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export const useGroupBuy = () => {
  const services = ref<Service[]>([])
  const router = useRouter()

  const fetchServices = async () => {
    try {
      const response = await serviceApi.getList()
      if (response.data.code === 200) {
        services.value = response.data.data
      }
    } catch (error) {
      console.error('获取团购列表失败:', error)
      ElMessage.error('获取团购列表失败')
    }
  }

  const goToDetail = (id: number) => {
    router.push(`/group-buy/${id}`)
  }

  onMounted(() => {
    fetchServices()
  })

  return {
    services,
    goToDetail
  }
} 